// pages/private/private.js
const ipUtil = require("../../utils/hostUtil")
const requestUtil = require("../../utils/requestUtil")
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
      token:'',
	  phone:'',
      userInfo:{},
	  viewShowed: true,
    },
	
	cancle: function () {
	    wx.setStorageSync('enws', '2');
	    this.setData({
	      viewShowed: true
	    })
	  },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function () {
        wx.setNavigationBarTitle({title: '个人资料'})
        // var token = app.globalData.token
        // var user = app.globalData.userInfo
        // console.log(user)
        // this.setData({
        //   token: token,
        //   userInfo: user
        // })
		this.loadPage()
    },
	
	loadPage:function(){
		var token=wx.getStorageSync('token')
		var user = app.globalData.userInfo
		var phone = wx.getStorageSync('phone')
		console.log(phone)
		console.log(user)
		if(token){
			this.setData({
				token: token,
				userInfo: user,
				phone:phone
			})
		}
	},

    logOut:function(){
		var token=wx.getStorageSync('token')
		var user=app.globalData.userInfo
		console.log(user)
		if(!token){
			wx.showToast({
				title:'请先登录'
			})
		}else{
        wx.showModal({
            title: '提示',
            content: '您确定要退出登录吗',
            success: function (res) {
              console.log(res)
              if (res.confirm) {
                wx.clearStorageSync('token')
                wx.clearStorageSync('user')
                wx.reLaunch({
                  url: '/pages/mys/mys',
                })
              }else{
              } 
            }
          })
		}
    },
    
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
		var enws = wx.getStorageSync('enws');
		var token = wx.getStorageSync('token')
			console.log("enws:", +enws);
			var that = this;
			if (enws != '2') { //判断能否授权  
			  that.setData({
				viewShowed: false,
			  })
			  console.log('判断能否授权');
			}
    },
	
	getPhoneNumber: function (e) { 
		console.log(e)
	    var that = this;
	    wx.checkSession({
	      success: function (res) {
			  console.log(res)
	        var ency = e.detail.encryptedData;
	        var iv = e.detail.iv;
	        that.setData({
	          viewShowed: true,
	        })
	        wx.setStorageSync('enws', '2');
	        if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
	
	        } else {
				wx.login({
					success(res){
						console.log(res)
						that.code=res.code
						wx.request({
							url:`${ipUtil.host}${requestUtil.getTel}`,
							data: {
							  code:that.code,
							  iv: iv,
							  encryptedData: ency
							},
							header: requestUtil.header,
							method:'POST',
							success(res){
								console.log(res)
								let phone = res.data.data
								that.setData({
									phone:phone
								})
								wx.setStorageSync('phone',phone)
							}
						})
					}
				})
	        }
	      }
	    });
	  },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    toaddress: function () {
        wx.navigateTo({
            url: '/pages/adress/adress',
        })
    }
})